package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.WebResource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private GraphInfoMapper graphInfoMapper ;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper ;

    @Autowired
    private LabelInfoMapper labelInfoMapper ;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper ;

    @Autowired
    private LeaseTermMapper leaseTermMapper ;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private RoomFacilityMapper roomFacilityMapper;

    @Autowired
    private RoomLabelMapper roomLabelMapper;

    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper ;

    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper ;

    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;

    @Override
    public int getRoomCountByApartmentId(Long apartmentId) {
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getApartmentId,apartmentId);
        return ((Long)count(lambdaQueryWrapper)).intValue();
    }

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return baseMapper.pageItem(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo() ;

        //1.获取房间的基本信息(包含公寓信息)
        RoomInfo roomInfo = this.getById(id);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentInfo(apartmentInfo);

        //2.获取图片列表
        List<GraphInfo> graphInfoList = graphInfoMapper
                .selectList(new LambdaQueryWrapper<GraphInfo>()
                        .eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId, id));
        roomDetailVo.setGraphVoList(graphInfoList.stream()
                .map(graphInfo -> GraphVo.builder().name(graphInfo.getName()).url(graphInfo.getUrl()).build())
                .collect(Collectors.toList()));

        //3.获取支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.getPaymentTypeListByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        //4.获取标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.getLableInfoListByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfoList);

        //5.获取配套列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityInfoListByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);

        //6.获取租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.getLeaseTermList(id);
        roomDetailVo.setLeaseTermList(leaseTermList);

        //7.获取属性值列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.getAttrValueVoListByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        //1.删除图片
        graphInfoMapper.delete(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType,ItemType.ROOM).eq(GraphInfo::getItemId,id)) ;
        //2.删除配套关系
        roomFacilityMapper.delete(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
        //3.删除标签关系
        roomLabelMapper.delete(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
        //4.删除属性关系
        roomAttrValueMapper.delete(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
        //5.删除租期关系
        roomLeaseTermMapper.delete(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
        //6.删除支付方式关系
        roomPaymentTypeMapper.delete(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));

        baseMapper.deleteById(id);
    }
}




